//
//  HWLoginViewController.m
//  HWPromoteProjectAPP
//
//  Created by 陈道斌 on 2020/2/18.
//  Copyright © 2020 陈道斌. All rights reserved.
//

#import "HWLoginViewController.h"
#import "HWTabBarController.h"
#import "HWPromoteProjectAPP-Swift.h"

@interface HWLoginViewController ()
@property (weak, nonatomic) IBOutlet UIView *zuiwaimianbeijingView;
@property (weak, nonatomic) IBOutlet UITextField *zhanghuTextField;
@property (weak, nonatomic) IBOutlet UITextField *mimaTextFeild;
@property (weak, nonatomic) IBOutlet UIButton *dengluBut;
@property (weak, nonatomic) IBOutlet UIImageView *icoiamgetouxiangView;

@end

@implementation HWLoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self setupUI];
    
    // com.HWan.HWPromoteProjectAPP
    
    
    self.icoiamgetouxiangView.layer.cornerRadius = 14;
    self.icoiamgetouxiangView.layer. masksToBounds = YES;
    
     NSDictionary *dic =  [[NSBundle mainBundle] infoDictionary];
      NSString *icostr =     dic[@"CFBundleIcons"][@"CFBundlePrimaryIcon"][@"CFBundleIconName"];
     //UIImage *appIcon = [UIImage imageNamed: [[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleIcons"] objectAtIndex:0]];
     UIImage  *appIcon = [UIImage imageNamed:icostr];
     self.icoiamgetouxiangView.image = appIcon;
    
    
}

- (void)setupUI {
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.zuiwaimianbeijingView.layer.shadowColor = [UIColor colorWithRed:113/255.0 green:152/255.0 blue:255/255.0 alpha:0.49].CGColor;
       self.zuiwaimianbeijingView.layer.shadowOffset = CGSizeMake(0,0);
       self.zuiwaimianbeijingView.layer.shadowOpacity = 1;
       self.zuiwaimianbeijingView.layer.shadowRadius = 12;
       self.zuiwaimianbeijingView.layer.cornerRadius = 6;
       self.zuiwaimianbeijingView.layer.masksToBounds = YES;
       self.zuiwaimianbeijingView.clipsToBounds = NO;
    
    self.dengluBut.layer.cornerRadius = 6;
    self.dengluBut.layer.masksToBounds = YES;
    
    
    //    self.zhanghuTextField.keyboardType = UIKeyboardTypeASCIICapable;
    
      self.mimaTextFeild.keyboardType = UIKeyboardTypeASCIICapable;
      self.mimaTextFeild.secureTextEntry = YES;
    
}


// 点击登录按钮
- (IBAction)dengluBut:(UIButton *)sender {
    
  
    
    
    if(!(self.zhanghuTextField.text.length > 0)) {
         
         [MBProgressHUD showError:@"请输入账号"];
         return;
     } else if (!(self.mimaTextFeild.text.length > 0)) {
         
         [MBProgressHUD showError:@"请输入密码"];
         return;
         
     }else if ([WMUtil isBlank:self.mimaTextFeild.text]){
         
         [MBProgressHUD showError:@"密码中不能有空格"];
         return;
     }
         
        
    
 
     NSString *user_nameStr = self.zhanghuTextField.text;
     NSString *passwdStr = self.mimaTextFeild.text;
     NSString *tStr = [NSString stringWithFormat:@"%ld",[WMUtil getTimeSp]];
     
     NSString *signStr = [NSString stringWithFormat:@"%@%@%@%@",user_nameStr,passwdStr,tStr,@"adesxloksy13s98adv"];
     NSString  *signStrMd5  =   [WMUtil md5HexDigest:signStr];
       
      [MBProgressHUD showMessage:@"正在登录"];
    

    
      NSDictionary *parametes = @{      @"ios" : @"1",
                                   @"ct" : @"app",
                                    @"ac" : @"login",
                                    @"username": user_nameStr,
                                    @"passwd" : passwdStr,
                                    @"t" : tStr,
                                    @"sign" :signStrMd5
                             
                                    };
    
    
    
    
//     {
//         ac = login;
//         ct = app;
//         ios = 1;
//         passwd = 123456;
//         sign = c8423b57d1a5045fd4e82d24d5d61d8c;
//         t = 1585190916;
//         username = demo;
//     }
//
     
       
          [HWWebUtils doGetWithParameters:parametes progress:nil success:^(id response) {
              [MBProgressHUD hideHUDForView:[UIApplication sharedApplication].keyWindow];
             

              NSString *codeStr =  [NSString stringWithFormat:@"%@",response[@"code"]];
              if ([codeStr intValue] == 1) {
             
                    NSLog(@"%@",response);
                     [MBProgressHUD showSuccess:@"登录成功"];
             
                    [HWLocallyStoredData setUserID:response[@"data"][@"uid"]];
                    [HWLocallyStoredData setUsername:response[@"data"][@"username"]];
                    [HWLocallyStoredData setHead:response[@"data"][@"head"]];
                    NSString *appStr = [NSString stringWithFormat:@"%@",response[@"app"]];
                  //   NSString *appStr = [NSString stringWithFormat:@"%@",@"3"];
                  
                    [HWLocallyStoredData setAppStr:appStr];
                  
                  
                  
                    HWTabBarController1 *viewcontr = [[HWTabBarController1 alloc]init];
                    
                  
                    kKeyWindow.rootViewController = viewcontr;
                  
                  
                  
                  
                  
                  


//                  if ([self.jizhuStr isEqualToString:@"1"]) {
//                       [HWLocallyStoredData setAccount:user_nameStr];
//
//                  }
                                 
                 
                    
                  
                   
              } else {
                  
                   [MBProgressHUD showError:response[@"message"]];
                                
            
                  
              }
              
         
              
          } failure:^(NSString *error) {
             // [SVProgressHUD dismiss];
              
              [MBProgressHUD showSuccess:@"登录失败"];
              [MBProgressHUD hideHUD];
              
              
          }];
    
    
    
     
    
    
    
    
    
    
    
    
//    HWTabBarController *viewcontr = [[HWTabBarController alloc]init];
//
//    kKeyWindow.rootViewController = viewcontr;
//
//
    
    
    
}



@end
